/**
 ******************************************************************************
 * @file    main.h
 * @author  MCU Application Team
 * @brief   Header for main.c file.
 *          This file contains the common defines of the application.
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by Puya under BSD 3-Clause license,
 * the "License"; You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                        opensource.org/licenses/BSD-3-Clause
 *
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under BSD 3-Clause license,
 * the "License"; You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                        opensource.org/licenses/BSD-3-Clause
 *
 ******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C"
{
#endif

/* Includes ------------------------------------------------------------------*/
#include "py32f0xx_hal.h"
#include "py32f0xx_hal_iwdg.h"
#include "string.h"
#include "cmsis_armcc.h"
#include <string.h>
#include <stdio.h>
#include "driver_key.h"
#include "driver_led.h"
#include "driver_radar.h"
#include "tick.h"
// #include "py32f0xx_hal_def.h"
// #include "py32f002xx_Start_Kit.h"
#define FRAME_HEAD 0xa5
#define FRAME_TAIL 0x5a
#define DEVICE_TYPE 0x03 // 雷达
#define CONTROL_OR_REPORT_CMD 0x02
#define SERVER_CONTROL_CMD 0x02
#define SERVER_TO_REPORT_CMD 0x03
#define SERVER_SET_INDICATOR_FUNCTION_CMD 0x04
#define SERVER_SET_POWER_ON_STATE_CMD 0x05
#define SERVER_GET_STATE_CMD 0x06
#define SERVER_REBOOT_CMD 0x99
#define VERSION 0x01

#define RETRY_FAILED_STR "try alot of time out,failed, not send"
#define RETRY_FAILED_2_STR "try 3 times, failed, not send"
#define RECEIVED_STR "receive sent ok"
#define RETRY_STR "retry send data to server\r\n"

#define DEBUG_UART_Rx_GPIO_CLK_ENABLE __HAL_RCC_GPIOB_CLK_ENABLE
#define DEBUG_UART_Rx_GPIO_PORT GPIOB
#define DEBUG_UART_Rx_GPIO_PIN GPIO_PIN_5

#define DEBUG_UART_Tx_GPIO_CLK_ENABLE __HAL_RCC_GPIOB_CLK_ENABLE
#define DEBUG_UART_Tx_GPIO_PORT GPIOB
#define DEBUG_UART_Tx_GPIO_PIN GPIO_PIN_4

#define QUERY_NET_STATE_TIME 60000
#define RETRY_SEND_WAIT_TIME 7000
#define RETRY_COUNT 10
#define Rx_Data_Buf_Size 100

#define STATE_INDEX 0x00
#define INDOCATOR_STATE_INDEX 0x01
#define POWER_ON_STATE_INDEX 0x02

  /* Private includes ----------------------------------------------------------*/
  typedef struct RelayState
  {
    GPIO_TypeDef *GPIOx;
    uint8_t pin;
    uint8_t id;
    uint8_t state;
  } RelayState;

  typedef enum
  {
    RELAY_OFF = 0x00U,
    RELAY_ON = 0x01U
  } RelayStateEnum;

  typedef struct
  {
    uint16_t Index;
    char data[Rx_Data_Buf_Size];
  } Buffer;

  typedef struct
  {
    uint8_t tryCount;
    bool isSucceed;
    bool reportNew;
    uint32_t lastTry;
  } SendState;

  /* Exported functions prototypes ---------------------------------------------*/
  void Error_Handler(void);
  void rebootSystem();
  extern void Reset_Handler(void);
  void centerParser();
  // 解析命令帧的函数
  void parseCommandFrame(uint8_t *frame, size_t frame_size);
  void sendCmd(char *cmd);
  void reportAllStateToServer();
  extern uint32_t tickMs;
  extern Buffer rxDataBuf;
  extern UART_HandleTypeDef uartHandle;
  extern uint8_t btIndicatorMode; // 0: 表示用于指示按键按压下去，1：表示用于指示继电器状态
  extern void (*okParser)(uint8_t *data, void (*succeedCalback)(uint8_t *data), void (*failedCalback)(uint8_t *data));
  extern void (*onSucceedCalback)(uint8_t *data);
  extern void (*onFailedCalback)(uint8_t *data);

  /* Exported functions prototypes ---------------------------------------------*/
  /* Private defines -----------------------------------------------------------*/

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */

/************************ (C) COPYRIGHT Puya *****END OF FILE****/
